﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;
using System.Drawing;

namespace TMDT_Nhom1A.UserControl_Admin
{
    public partial class TranportWebserviceManager : System.Web.UI.UserControl
    {
        private static List<TRANSPORT_WEBSERVICE> transportWsInfo = new List<TRANSPORT_WEBSERVICE>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                transportWsInfo = TransportWebserviceBUS.getAllTransportWebservice();
                gvTransportWSList.DataSource = transportWsInfo;
                gvTransportWSList.DataBind();
            }
        }

        protected void btAdd_Click(object sender, ImageClickEventArgs e)
        {
            List<TRANSPORT_WEBSERVICE> temp = new List<TRANSPORT_WEBSERVICE>();
            TRANSPORT_WEBSERVICE transWs = new TRANSPORT_WEBSERVICE();
            transWs.TRANSPORT_WS_ID = 5;

            temp.Add(transWs);
            DlAddUpdateTransportWS.EditItemIndex = -1;
            DlAddUpdateTransportWS.DataSource = temp;
            DlAddUpdateTransportWS.DataBind();           
            DlAddUpdateTransportWS.Visible = true;

           
           
        }    

        protected void DlAddUpdateTransportWS_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                TextBox tblink = (TextBox)e.Item.FindControl("tblink");
                TextBox tbTransportName = (TextBox)e.Item.FindControl("tbTransportName");
                TextBox tbTransprotDescription = (TextBox)e.Item.FindControl("tbTransportDescription");
                

                TRANSPORT_WEBSERVICE transWs = new TRANSPORT_WEBSERVICE();
                transWs.TransportCompanyName = tbTransportName.Text;
                transWs.TransportUrl = tblink.Text;
                transWs.Description = tbTransprotDescription.Text;

                bool result = TransportWebserviceBUS.insertTransportWebservice(transWs);
                if (result == true)
                {
                    transportWsInfo.Add(transWs);
                    //refesh lai gridview va hien thong bao
                    gvTransportWSList.DataSource = TransportWebserviceBUS.getAllTransportWebservice();
                    gvTransportWSList.DataBind();
                    DlAddUpdateTransportWS.Visible = false;
                   
                }
                else
                {
                    LbThongBao.Text = "Thêm Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }

            if (e.CommandName == "Update")
            {
                

                Label lbtransportWsID = (Label)e.Item.FindControl("TransportID");
                TextBox tblink = (TextBox)e.Item.FindControl("tblink");
                TextBox tbTransportName = (TextBox)e.Item.FindControl("tbTransportName");
                TextBox tbTransprotDescription = (TextBox)e.Item.FindControl("tbTransportDescription");

                TRANSPORT_WEBSERVICE newTransportWS = new TRANSPORT_WEBSERVICE();
                newTransportWS.TRANSPORT_WS_ID = int.Parse(lbtransportWsID.Text);
                newTransportWS.TransportCompanyName = tbTransportName.Text;
                newTransportWS.TransportUrl = tblink.Text;
                newTransportWS.Description = tbTransprotDescription.Text;

                bool result = TransportWebserviceBUS.updateTransportWebservice(newTransportWS);
                if (result == true)
                {
                    //refesh lai gridview va hien thong bao
                    gvTransportWSList.DataSource = TransportWebserviceBUS.getAllTransportWebservice();
                    gvTransportWSList.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Cập Nhật Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }

            if (e.CommandName == "Cancel")
            {
                DlAddUpdateTransportWS.Visible = false;
            }


        }

        protected void gvTransportWSList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                
                //tim thong tin trong list
                List<TRANSPORT_WEBSERVICE> temp = new List<TRANSPORT_WEBSERVICE>();
                for (int i = 0; i < transportWsInfo.Count; i++)
                {
                    if (transportWsInfo[i].TRANSPORT_WS_ID == int.Parse(e.CommandArgument.ToString()))
                    {
                        temp.Add(transportWsInfo[i]);
                    }
                }

                DlAddUpdateTransportWS.EditItemIndex = 0;
                DlAddUpdateTransportWS.Visible = true;
                DlAddUpdateTransportWS.DataSource = temp;
                DlAddUpdateTransportWS.DataBind();

                
            }

            if (e.CommandName == "btDelete")
            {
                int transportID = int.Parse(e.CommandArgument.ToString());
                bool result = TransportWebserviceBUS.deleteTransportWebservice(transportID);
                if (result == true)
                {
                    for (int i = 0; i < transportWsInfo.Count; i++)
                    {
                        if (transportWsInfo[i].TRANSPORT_WS_ID == transportID)
                        {
                            transportWsInfo.RemoveAt(i);
                        }
                    }
                    //refesh lai gridview va hien thong bao
                    gvTransportWSList.DataSource = TransportWebserviceBUS.getAllTransportWebservice();
                    gvTransportWSList.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Xoá Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }
        }

        protected void gvTransportWSList_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }
    }
}